from django.shortcuts import render
from app.models.order import Orders,Menu
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.http import HttpResponse

# 订单列表页
def order_list(request):

    menus = Orders.menus
    type = request.GET.get('type')
    if type is None:
        type = 0
  # 待收货订单
    orderList_0 = Orders.objects.filter(status='待收货').order_by("-time_end")
    pagenum_0 = 5  # 每页显示数据条数
    paginator_0 = Paginator(orderList_0, pagenum_0)  # 分页的对象，每页显示的条数
    page_0 = request.GET.get('page_0', 1)  # 接收网页中的page值，即页码数，默认为1
    currentPage_0 = int(page_0)
    page_start_0 = (currentPage_0 - 1) * pagenum_0  # 当前页起始的序列号
    try:
        orderList_0 = paginator_0.page(page_0)  # 括号里的是页数，显示指定页码的数据，动态显示数据，所以不能写死了
    except PageNotAnInteger:
        orderList_0 = paginator_0.page(1)
    except EmptyPage:  # 如果超过了页码范围，就把最后的页码显示出来，
        orderList_0 = paginator_0.page(paginator_0.num_pages)

    # 已完成订单
    orderList_1 = Orders.objects.filter(status='已完成').order_by("-time_end")
    pagenum_1 = 5
    paginator_1 = Paginator(orderList_1, pagenum_1)
    page_1 = request.GET.get('page_1', 1)
    currentPage_1 = int(page_1)
    page_start_1 = (currentPage_1 - 1) * pagenum_1
    try:
        orderList_1 = paginator_1.page(page_1)
    except PageNotAnInteger:
        orderList_1 = paginator_1.page(1)
    except EmptyPage:
        orderList_1 = paginator_1.page(paginator_1.num_pages)

    # 待支付订单
    orderList_2 = Orders.objects.filter(status='待付款').order_by("-time_end")
    pagenum_2 = 5
    paginator_2 = Paginator(orderList_2, pagenum_2)
    page_2 = request.GET.get('page_2', 1)
    currentPage_2 = int(page_2)
    page_start_2 = (currentPage_2 - 1) * pagenum_2
    try:
        orderList_2 = paginator_2.page(page_2)
    except PageNotAnInteger:
        orderList_2 = paginator_2.page(1)
    except EmptyPage:
        orderList_2 = paginator_2.page(paginator_2.num_pages)

    # 订单数据字典
    order_dict = {'orderList_0': orderList_0, 'orderList_1': orderList_1, 'orderList_2': orderList_2, 'page_start_0': page_start_0,
                  'page_start_1': page_start_1, 'page_start_2': page_start_2, 'type': type, 'menus': menus}
    # 渲染页面
    return render(request, 'visites/orders.html', order_dict)

#订单详情页
def order_detail(request):
    order_id = request.GET.get('id')
    order = Orders.objects.get(id=order_id)
    order_detail_dict = {'order': order}
    return render(request, 'visites/order-detail.html', order_detail_dict)


#用户个人订单页
def orderUser(request):
    user_id = request.GET.get('id')
    menus = Orders.menus
    type = request.GET.get('type')

    page_size = 5

    if type is None:
        type = 0
    orderList_0 = Orders.objects.filter(status='待收货',user_id=user_id).order_by("-time_end")
    pagenum_0 = page_size  # 每页显示数据条数
    paginator_0 = Paginator(orderList_0, pagenum_0)  # 分页的对象，每页显示的条数
    page_0 = request.GET.get('page_0', 1)  # 接收网页中的page值，即页码数，默认为1
    currentPage_0 = int(page_0)
    page_start_0 = (currentPage_0 - 1) * pagenum_0  # 当前页起始的序列号
    try:
        orderList_0 = paginator_0.page(page_0)  # 括号里的是页数，显示指定页码的数据，动态显示数据，所以不能写死了
    except PageNotAnInteger:
        orderList_0 = paginator_0.page(1)
    except EmptyPage:  # 如果超过了页码范围，就把最后的页码显示出来，
        orderList_0 = paginator_0.page(paginator_0.num_pages)

    # 已完成订单
    orderList_1 = Orders.objects.filter(status='已完成',user_id=user_id).order_by("-time_end")
    pagenum_1 = page_size
    paginator_1 = Paginator(orderList_1, pagenum_1)
    page_1 = request.GET.get('page_1', 1)
    currentPage_1 = int(page_1)
    page_start_1 = (currentPage_1 - 1) * pagenum_1
    try:
        orderList_1 = paginator_1.page(page_1)
    except PageNotAnInteger:
        orderList_1 = paginator_1.page(1)
    except EmptyPage:
        orderList_1 = paginator_1.page(paginator_1.num_pages)

    # 待付款订单
    orderList_2 = Orders.objects.filter(status='待付款',user_id=user_id).order_by("-time_end")
    pagenum_2 = page_size
    paginator_2 = Paginator(orderList_2, pagenum_2)
    page_2 = request.GET.get('page_2', 1)
    currentPage_2 = int(page_2)
    page_start_2 = (currentPage_2 - 1) * pagenum_2
    try:
        orderList_2 = paginator_2.page(page_2)
    except PageNotAnInteger:
        orderList_2 = paginator_2.page(1)
    except EmptyPage:
        orderList_2 = paginator_2.page(paginator_2.num_pages)
    # 订单数据字典
    order_dict = {'orderList_0': orderList_0, 'orderList_1': orderList_1, 'orderList_2': orderList_2,
                  'page_start_0': page_start_0,
                  'page_start_1': page_start_1, 'page_start_2': page_start_2, 'type': type, 'menus': menus, "user_id": user_id}
    # 渲染页面
    return render(request, 'visites/orders.html', order_dict)